package com.shigu.user.action;

import com.shigu.MallResponse;
import com.shigu.TokenRequest;
import com.shigu.kefu.service.ContactService;
import com.shigu.kefu.vo.ConcactInfoResponse;
import com.shigu.kefu.vo.QuickUpgradeResponse;
import com.shigu.paypwd.util.SendMsgUtil;
import com.shigu.exceptions.WapException;
import com.shigu.paypwd.bo.OldPwdRequest;
import com.shigu.paypwd.bo.PwdRequest;
import com.shigu.paypwd.service.PwdService;
import com.shigu.paypwd.vo.HasPwdResponse;
import com.shigu.paypwd.vo.MoneyResponse;
import com.shigu.paypwd.vo.PersonCenterResponse;
import com.shigu.phoneCode.enums.PhoneCodeType;
import com.shigu.phoneCode.service.PhoneCodeService;
import com.shigu.user.bo.LoginPwdRequest;
import com.shigu.user.bo.NickRequest;
import com.shigu.user.bo.PhoneCodeRequest;
import com.shigu.user.bo.PhoneRequest;
import com.shigu.user.service.MallTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

@Controller
@RequestMapping("/wapsjson")
public class PersonCenterController {
    @Autowired
    PwdService pwdService;
    @Autowired
    SendMsgUtil sendMsgUtil;
    @Autowired
    PhoneCodeService phoneCodeService;
    @Autowired
    MallTokenService mallTokenService;
    @Autowired
    ContactService contactService;

    @RequestMapping("checkPhoneCode")
    @ResponseBody
    public MallResponse checkPhoneCode (@Valid PhoneCodeRequest request, BindingResult result, HttpSession session) throws WapException {
        if (result.hasErrors()) {
            throw new WapException(result.getFieldError().getDefaultMessage());
        }
        if (!phoneCodeService.checkPhoneCode(request.getTelephone(),request.getPhoneCode(), PhoneCodeType.PAYPASSOWRD,15*60*1000)) {
            throw new WapException("验证码错误");
        }
        return new MallResponse().success();
   }

    @RequestMapping("sendPhoneCode")
    @ResponseBody
    public MallResponse sendPhoneCode(@Valid PhoneRequest request, BindingResult result, HttpSession session) throws WapException {
        if (result.hasErrors()) {
            throw new WapException(result.getFieldError().getDefaultMessage());
        }
        return pwdService.sendPhoneCode(request.getTelephone());
    }

    @RequestMapping("setPayPwd")
    @ResponseBody
    public MallResponse setPayPwd(PwdRequest request) throws WapException {
        Long userId = mallTokenService.selUserIdByToken(request.getToken());
        return pwdService.setPayPwd(userId,request.getPayPwd());
    }

    @RequestMapping("walletMoney")
    @ResponseBody
    public MoneyResponse walletMoney(TokenRequest request) throws WapException {
        Long userId = mallTokenService.selUserIdByToken(request.getToken());
        return pwdService.walletMoney(userId);
    }

    @RequestMapping("hasPayPwd")
    @ResponseBody
    public HasPwdResponse hasPayPwd(TokenRequest request) throws WapException {
        Long userId = mallTokenService.selUserIdByToken(request.getToken());
        return pwdService.hasPayPwd(userId);
    }

    @RequestMapping("personCenter")
    @ResponseBody
    public PersonCenterResponse personCenter(TokenRequest request) throws WapException {
        Long userId = mallTokenService.selUserIdByToken(request.getToken());
        return pwdService.personCenter(userId);
    }

    @RequestMapping("checkOldPwd")
    @ResponseBody
    public MallResponse checkOldPwd(OldPwdRequest request) throws WapException {
        Long userId = mallTokenService.selUserIdByToken(request.getToken());
        return pwdService.checkOldPwd(userId,request.getOldPwd());
    }

    @RequestMapping("changeLoginPwd")
    @ResponseBody
    public MallResponse changeLoginPwd(@Valid LoginPwdRequest request, BindingResult result) throws WapException {
        if (result.hasErrors()) {
            throw new WapException(result.getFieldError().getDefaultMessage());
        }
        Long userId = mallTokenService.selUserIdByToken(request.getToken());
        return pwdService.changeLoginPwd(userId,request.getOldPassword(),request.getNewPassword());
    }

    @RequestMapping("changeNick")
    @ResponseBody
    public MallResponse changeNick(@Valid NickRequest request, BindingResult result) throws WapException {
        if (result.hasErrors()) {
            throw new WapException(result.getFieldError().getDefaultMessage());
        }
        Long userId = mallTokenService.selUserIdByToken(request.getToken());
        return pwdService.changeNick(userId,request.getNick());
    }

    @RequestMapping("getContactInfo")
    @ResponseBody
    public ConcactInfoResponse getContactInfo(){
        return contactService.getContactInfo();
    }

    @RequestMapping("getQuickUpgradeContact")
    @ResponseBody
    public QuickUpgradeResponse getQuickUpgradeContact(){
        return contactService.quickUpgrade();
    }
}
